package com.xyu.gulimall.product.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.Query;

import com.xyu.gulimall.product.dao.SkuInfoDao;
import com.xyu.gulimall.product.entity.SkuInfoEntity;
import com.xyu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }
    //保存sku基本信息

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    /**
     * 商品管理sku检索
     *请求参数
     * @param params {
     *               page: 1,//当前页码
     *               limit: 10,//每页记录数
     *               sidx: 'id',//排序字段
     *               order: 'asc/desc',//排序方式
     *               key: '华为',//检索关键字
     *               catelogId: 0,
     *               brandId: 0,
     *               min: 0,
     *               max: 0
     *               }
     * @return
     */
    @Override
    public PageUtils queryPageByCondtion(Map<String, Object> params) {
        //创建条件构造器
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        //如果key不为空则or连接判断
        if(!StringUtils.isEmpty(key)){
            wrapper.and((w) ->{
                w.eq("sku_id",key).or().eq("spu_id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String)params.get("catelogId");
        System.out.println(!StringUtils.isEmpty(catelogId));//
        if(!StringUtils.isEmpty(catelogId)){
            wrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String)params.get("brandId");

        System.out.println(!StringUtils.isEmpty(brandId));//
        if(!StringUtils.isEmpty(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        //价格区间
        String min = (String)params.get("min");
        String max = (String)params.get("max");
        int Min = Integer.parseInt(min);
        int Max = Integer.parseInt(max);
        //如果Min<Max取区间
        if (Min<Max&&Min!=Max){
            System.out.println(1);
            wrapper.between("price",min,max);
            //如果Min大于0 且Max等于0 则执行大于等于比较
        }else if(Min>0&&Max==0) {
            System.out.println(2);
            wrapper.ge("price", min);
        }
        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

    /**
     * SpuInfoController中up方法调用
     * 方法功能：根据spuId查询对应spu的sku
     * @param spuId
     */
    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        List<SkuInfoEntity> spuInfo = baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return spuInfo;
    }
}
